package com.example.project.service.impl;

import com.example.project.common.MyTool;
import com.example.project.dao.SafetyInspeBinDao;
import com.example.project.dao.SysMessageDao;
import com.example.project.entity.*;
import com.example.project.dao.SafetyInspeDao;
import com.example.project.service.SafetyInspeService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (SafetyInspe)表服务实现类
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@Service("safetyInspeService")
public class SafetyInspeServiceImpl implements SafetyInspeService {
    @Resource
    private SafetyInspeDao safetyInspeDao;
    @Resource
    private SafetyInspeBinDao safetyInspeBinDao;
@Resource
 private SysMessageDao sysMessageDao;
    private MyTool myTool = new MyTool();
    /**
     * 通过ID查询单条数据
     *
     * @param safetyInspeId 主键
     * @return 实例对象
     */
    @Override
    public SafetyInspe queryById(Integer safetyInspeId) {
        return this.safetyInspeDao.queryById(safetyInspeId);
    }

    /**
     * 分页查询
     *
     * @param safetyInspe 筛选条件
     * @param pageRequest 分页对象
     * @param startTime
     * @param endTime
     * @return 查询结果
     */
    @Override
    public Page<SafetyInspe> queryByPage(SafetyInspe safetyInspe, PageRequest pageRequest, Date startTime, Date endTime) {
        long total = this.safetyInspeDao.count(safetyInspe);
        return new PageImpl<>(this.safetyInspeDao.queryAllByLimit(safetyInspe, pageRequest, startTime, endTime), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param safetyInspe 实例对象
     * @return 实例对象
     */
    @Override
    public SafetyInspe insert(SafetyInspe safetyInspe) {

        //生成创建时间
        Date n = myTool.dateNow();
        safetyInspe.setCreationTime(n);
        this.safetyInspeDao.insert(safetyInspe);
        return safetyInspe;
    }

    /**
     * 修改数据
     *
     * @param safetyInspe 实例对象
     * @return 实例对象
     */
    @Override
    public SafetyInspe update(SafetyInspe safetyInspe) {
        Date dateNew = myTool.dateNow();
        //自动更新--更新时间
        safetyInspe.setUpdateTime(dateNew);
        this.safetyInspeDao.update(safetyInspe);
        return this.queryById(safetyInspe.getSafetyInspeId());
    }

    /**
     * 通过主键删除数据
     *
     * @param safetyInspeId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer safetyInspeId) {
        return this.safetyInspeDao.deleteById(safetyInspeId) > 0;
    }
    /**
     * 删除成功进入回收站
     * 失败
     */
    @Override
    public boolean deleteById1(Integer safetyInspeId) {
        SafetyInspe safetyInspe = this.safetyInspeDao.queryById(safetyInspeId);
        safetyInspe.setEntRecBin(2);
        //创建回收站数据
        SafetyInspeBin safetyInspeBin = new SafetyInspeBin();
        safetyInspeBin.setSafetyInspeId(safetyInspeId);
        //生成当前删除时的时间
        safetyInspeBin.setDeleteTime(myTool.dateNow());
        //绑定
        this.safetyInspeBinDao.insert(safetyInspeBin);

        return this.safetyInspeDao.update(safetyInspe)>0;
    }

    @Override
    public SafetyInspe update1(SafetyInspe data) { //数据库里的数据
        SafetyInspe safetyInspe = this.safetyInspeDao.queryById(data.getSafetyInspeId());
        //报警信息
        String pj = null;

        Integer x = data.getRectification();
        switch (x){
            case 2:{
              pj = safetyInspe.getRecordName()+"请在"+ data.getLimTime()+"之前这个";
                break;}
            case 3:{
               pj = data.getRecordName()+"已整改";
            }
            default:break;
        }
        if (pj != null){
            SysMessage sysMessage = myTool.generateMessage(pj);
            this.sysMessageDao.insert(sysMessage);
        }
        return update(data);
    }


}
